﻿
var baseUrl = "../../BusinessTier/Handlers/UserHandler.ashx?action=";
var add = "Add";
var del = "Delete";
var update = "Update";
var get = "Get";
var url;
function newUser() {
    $('#dlg').dialog('open').dialog('setTitle', '创建用户');
    //$('#fm').form('clear');
    $("input[name='UserName']").val("");
    $("input[name='Password']").val("");
    $("input[name='IsAdmin']").each(function () {
        if ($(this).val() == "false") {
            $(this).attr("checked", "checked")
        }
    })
    url = baseUrl + add;
}

function GetNewRow(row) {
    var containsProFileds = row.ContainsProFileds;
    var temp = $.parseJSON("{" + containsProFileds + "}");
    var user = {
        "Id": row.id,
        "UserName": row.UserName,
        "Password": row.Password,
        "IsAdmin": row.IsAdmin,
        "QQ": temp.QQ,
        "Wangwang": temp.Wangwang,
        "ZhifubaoAccount": temp.ZhifubaoAccount,
        "ZhifubaoName": temp.ZhifubaoName,
        "RebateZhifubao": temp.RebateZhifubao,
        "RebateZhifubaoName": temp.RebateZhifubaoName,
        "CaifutongAccount": temp.CaifutongAccount,
        "CaifutongName": temp.CaifutongName,
        "ShopID": temp.ShopID,
        "Cash": temp.Cash,
        "Cellphone": temp.Cellphone,
        "Fixphone": temp.Fixphone,
        "ReceiveName": temp.ReceiveName,
        "ReceiveAddress": temp.ReceiveAddress,
        "ReceiveCellphone": temp.ReceiveCellphone,
        "ReceiveFixphone": temp.ReceiveFixphone,
        "Date": temp.Date,
        "Address": temp.Address
    }
    return user;
}

function editUser() {
    if ($("#isAdmin").val() == "False") {
        setDisabled();
    }
    var row = $('#dg').datagrid('getSelected');
    //var containsProFileds = row.ContainsProFileds;
    if (!!row) {
        var row = GetNewRow(row);  //$.parseJSON("{" + containsProFileds + "}"); //test(containsProFileds);
    }
    if (row) {
        $('#dlg').dialog('open').dialog('setTitle', '更新用户');
        $('#fm').form('clear');
        $('#fm').form('load', row);
        //url = '../Handlers/UserHandler/UpdateUserHandler.ashx?id=' + row.Id;
        url = baseUrl + update + "&id=" + row.Id;
    }
}



function saveUser() {
    setEnabled();
    $('#fm').form('submit', {
        url: url,
        onSubmit: function () {
            return $(this).form('validate');
        },
        success: function (result) {
            var result = eval('(' + result + ')');
            if (!!result.Repeater) {
                jQuery.messager.alert('提示:', '该用户名已经存在，请更改!');
                return;
            }
            if (result.errorMsg) {
                $.messager.show({
                    title: 'Error',
                    msg: result.errorMsg
                });
            } else {
                $('#dlg').dialog('close');        // close the dialog
                $('#dg').datagrid('reload');    // reload the user data
            }
        }
    });
}
function destroyUser() {
    var row = $('#dg').datagrid('getSelected');
    if (row) {
        $.messager.confirm('确认', '您确定要删除该用户吗?', function (r) {
            if (r) {
                if (row.UserName == $.trim($("#userName").val())) {
                    jQuery.messager.alert('注意:', '该账户为管理员,您不能删除！');
                    return;
                }
                $.post(baseUrl+del, { id: row.id }, function (result) {

                    if (result.success) {

                        $('#dg').datagrid('reload');    // reload the user data
                    } else {
                        $.messager.show({    // show error message
                            title: 'Error',
                            msg: result.errorMsg
                        });
                    }
                }, 'json');

            }
            $('#dg').datagrid('reload');
        });
    }

}


function setDisabled() {
    $("input[name='IsAdmin']").attr("disabled", "disabled").validatebox({
        required: false
    });
    $("input[name='UserName']").attr("disabled", "disabled").validatebox({
        required: false
    });
    $("select[name='QQ']").attr("disabled", "disabled");
    $("select[name='Wangwang']").attr("disabled", "disabled");
    $("select[name='ZhifubaoAccount']").attr("disabled", "disabled");
    $("select[name='ZhifubaoName']").attr("disabled", "disabled");
    $("select[name='RebateZhifubao']").attr("disabled", "disabled");
    $("select[name='RebateZhifubaoName']").attr("disabled", "disabled");
    $("select[name='CaifutongAccount']").attr("disabled", "disabled");
    $("select[name='CaifutongName']").attr("disabled", "disabled");
    $("select[name='ShopID']").attr("disabled", "disabled");
    $("select[name='Cash']").attr("disabled", "disabled");
    $("select[name='Cellphone']").attr("disabled", "disabled");
    $("select[name='Fixphone']").attr("disabled", "disabled");
    $("select[name='ReceiveName']").attr("disabled", "disabled");
    $("select[name='ReceiveAddress']").attr("disabled", "disabled");
    $("select[name='ReceiveCellphone']").attr("disabled", "disabled");
    $("select[name='ReceiveFixphone']").attr("disabled", "disabled");
    $("select[name='Date']").attr("disabled", "disabled");
    $("select[name='Address']").attr("disabled", "disabled");
}

function setEnabled() {
    $("input[name='IsAdmin']").attr("disabled", false).validatebox({
        required: false
    });
    $("input[name='UserName']").attr("disabled", false).validatebox({
        required: false
    });
    $("select[name='QQ']").attr("disabled", false);
    $("select[name='Wangwang']").attr("disabled", false);
    $("select[name='ZhifubaoAccount']").attr("disabled", false);
    $("select[name='ZhifubaoName']").attr("disabled", false);
    $("select[name='RebateZhifubao']").attr("disabled", false);
    $("select[name='RebateZhifubaoName']").attr("disabled", false);
    $("select[name='CaifutongAccount']").attr("disabled", false);
    $("select[name='CaifutongName']").attr("disabled", false);
    $("select[name='ShopID']").attr("disabled", false);
    $("select[name='Cash']").attr("disabled", false);
    $("select[name='Cellphone']").attr("disabled", false);
    $("select[name='Fixphone']").attr("disabled", false);
    $("select[name='ReceiveName']").attr("disabled", false);
    $("select[name='ReceiveAddress']").attr("disabled", false);
    $("select[name='ReceiveCellphone']").attr("disabled", false);
    $("select[name='ReceiveFixphone']").attr("disabled", false);
    $("select[name='Date']").attr("disabled", false);
    $("select[name='Address']").attr("disabled", false);
}